﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace LibService.DAO
{
    public class TangTuDongDAO
    {
        public static string TangMa(string strID, string field, string table, int length)
        {
            string IDCode = "";
            SQLConnect cnn = new SQLConnect();
            DataTable dt = new DataTable();
           // dataHelper.connect();
            string query = "SELECT MAX(" + field.Trim() + ") FROM " + table.Trim();
            dt = cnn.ExecuteQuery(query, CommandType.Text);
            //SqlCommand cmd = new SqlCommand(query,cnn);
            //SqlDataReader dr = cmd.ExecuteReader();
            //while (dr.Read())
            //{
            //    IDCode = dr[0].ToString();
            //}

            if (IDCode != "")
            {
                int chieudai = length - strID.Length;
                IDCode = IDCode.Substring(strID.Length, chieudai);
                int iso = int.Parse(IDCode);
                iso = iso + 1;
                int dem = strID.Length + iso.ToString().Length;//cho biet so ID va so ma phat sinh la bao nhieu ky tu de
                //vong for nay vao so 0
                IDCode = "";
                for (int i = 1; i <= length - dem; i++)
                {
                    IDCode = IDCode + "0";
                }
                IDCode = strID + IDCode + iso.ToString();
            }
            else
            {
                for (int i = 1; i < length - strID.Length; i++)
                {
                    IDCode = IDCode + "0";
                }
                IDCode = strID + IDCode + "1";
            }


            return IDCode;
        }
    }
}